package com.yaodian100.ec.activity;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.crm.activities.entity.PromotionOuter;
import com.yaodian100.crm.activities.entity.PromotionPresent;
import com.yaodian100.crm.activities.service.PromotionOuterService;
import com.yaodian100.crm.activities.service.PromotionPresentService;
import com.yaodian100.ec.common.EcConstant;

public class PromotionOuterAction extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Logger logger = LoggerFactory.getLogger(PromotionOuterAction.class);

	public PromotionOuterService promotionOuterService;
	public PromotionPresentService promotionPresentService;
	private SimplePager pager;
	private List<PromotionOuter> promotionOuterList;
	private List<PromotionPresent> promotionPresentList;
	private PromotionOuter promotionOuter;
	private PromotionPresent promotionPresent;

	private String titleName;
	private String startDate;
	private String endDate;
	private String status;
	private String sendWay;
	private String startTime;
	private String endTime;
	private String description;
	private String domainSrc;
	private String price;
	private String[] qty;
	private String[] specId;
	private Menu menuChannel;
	private Menu menuDomain;
	@Autowired
	private MenuService menuService;
	private Menu menuBudget;
	private String result;
	private String del;
	private String giftId;
	private String produtId;
	private String str;

	/**
	 * 修改启用状态
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String updateStart() throws ParseException {
		PromotionOuter promotionOuter = new PromotionOuter();
		CommonCriteria criteria = new CommonCriteria();
		if (StringUtils.isNotBlank(giftId)) {
			criteria.addRlike("id", giftId);

			int totalSize = promotionOuterService.getListSize(criteria).intValue();
			pager.setTotalSize(totalSize);
			promotionOuterList = promotionOuterService.getList(pager.getPageRecord() * pager.getCurrentPage(),
					pager.getPageRecord(), criteria, null);

		}
		promotionOuter = promotionOuterList.get(0);

		if (StringUtils.isNotBlank(status)) {
			if ("1".equals(status)) {
				CommonCriteria criteria1 = new CommonCriteria();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				criteria1.addGe("endDate", format.parse(sdf.format(promotionOuter.getStartDate())));
				criteria1.addEq("status", status);
				criteria1.addRlike("domainSrc", promotionOuter.getDomainSrc());

				int totalSize = promotionOuterService.getListSize(criteria1).intValue();
				if (totalSize < 1) {
					promotionOuter.setStatus(status);
					promotionOuterService.update(promotionOuter);
				} else {
					result = "error";
				}
			} else {

				promotionOuter.setStatus(status);
				promotionOuterService.update(promotionOuter);
			}

		}

		return "list";

	}

	/**
	 * 保存数据
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String save() throws ParseException {
		CommonCriteria criteria = new CommonCriteria();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotBlank(startDate)) {
			criteria.addGe("endDate", format.parse(startDate));
		}
		criteria.addEq("status", "1");
		criteria.addRlike("domainSrc", domainSrc);

		int totalSize = promotionOuterService.getListSize(criteria).intValue();
		if (totalSize < 1) {
			promotionOuter = new PromotionOuter();
			DateFormat df = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
			promotionOuter.setStartDate(df.parse(startDate + " " + startTime + ":00"));
			promotionOuter.setEndDate(df.parse(endDate + " " + endTime + ":00"));
			promotionOuter.setCreateDate(new Date());
			promotionOuter.setCreateUser(AdminHelper.getUser().getUsername());
			promotionOuter.setDescription(description);// 描述
			promotionOuter.setDomainSrc(domainSrc);// 区域
			promotionOuter.setTitleName(titleName);// 标题
			promotionOuter.setStatus(status);// 启用
			promotionOuter.setSendWay(sendWay);
			promotionOuter.setPrice(new BigDecimal(price));// 价格
			promotionOuterService.savemain(promotionOuter);
			if (specId != null && qty != null) {
				for (int i = 0; i < specId.length; i++) {
					promotionPresent = new PromotionPresent();
					promotionPresent.setPrId(promotionOuter.getId());
					promotionPresent.setSpecId(Long.valueOf(specId[i]));
					promotionPresent.setQty(Integer.valueOf(qty[i]));
					promotionPresent.setAfnjQty(0);
					promotionPresentService.savegift(promotionPresent);
				}
			}
		} else {
			result = "error";
		}
		return "list";

	}

	public String jsonStr() {
		try {

			str = promotionOuterService.fingProductName(produtId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsonChargePayment";
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		CommonCriteria criteria = new CommonCriteria();
		if (StringUtils.isNotBlank(titleName)) {

			criteria.addRlike("titleName", titleName);
		}

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotBlank(startDate)) {
			criteria.addGe("startDate", format.parse(startDate));
		}
		if (StringUtils.isNotBlank(endDate)) {
			criteria.addLe("endDate", format.parse(endDate));
		}
		if (StringUtils.isNotBlank(status)) {
			criteria.addEq("status", status);
		}

		int totalSize = promotionOuterService.getListSize(criteria).intValue();
		pager.setTotalSize(totalSize);
		promotionOuterList = promotionOuterService.getList(pager.getPageRecord() * pager.getCurrentPage(),
				pager.getPageRecord(), criteria, null);

		return "list";
	}

	public String detailed() {
		CommonCriteria criteria = new CommonCriteria();
		if (StringUtils.isNotBlank(giftId)) {
			criteria.addRlike("id", giftId);

			int totalSize = promotionOuterService.getListSize(criteria).intValue();
			pager.setTotalSize(totalSize);
			promotionOuterList = promotionOuterService.getList(pager.getPageRecord() * pager.getCurrentPage(),
					pager.getPageRecord(), criteria, null);

		}
		if (StringUtils.isNotBlank(giftId)) {

			promotionPresentList = promotionPresentService.findByPrId(giftId);

		}
		return "show";
	}

	/**
	 * 载入新增活动资料页
	 * 
	 * @return
	 */
	public String add() {
		// jdGiftActivities = new JDGiftActivities();
		// Calendar cal = DateUtil.getToday();
		// cal.add(Calendar.DATE, 7);
		// cal.set(Calendar.HOUR_OF_DAY, 0);
		// cal.set(Calendar.MINUTE, 0);
		// jdGiftActivities.setCreateDate(cal.getTime());
		// cal.add(Calendar.MONTH, 1);
		// jdGiftActivities.setEndDate(DateUtil.getLastTimestamp(Calendar.DATE, cal.getTime()));
		// jdGiftActivities.addTypeExtend(new JDGiftActivitiesAction());
		return "edit";
	}

	/**
	 * 进入赠品行销管理页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		return "list";
	}

	@Override
	public void prepare() throws Exception {
		if (null == pager) {
			pager = new SimplePager();
		}

		if (menuChannel == null) {
			menuChannel = menuService.get("Channel.Name").clone();
			menuChannel.addOption("yaodian100", "耀点100");
		}
		if (menuDomain == null) {
			menuDomain = menuService.get("Site.domain");
		}
		if (menuBudget == null) {
			menuBudget = menuService.get("Budget");
		}

	}

	public PromotionOuterService getPromotionOuterService() {
		return promotionOuterService;
	}

	public void setPromotionOuterService(PromotionOuterService promotionOuterService) {
		this.promotionOuterService = promotionOuterService;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public List<PromotionOuter> getPromotionOuterList() {
		return promotionOuterList;
	}

	public void setPromotionOuterList(List<PromotionOuter> promotionOuterList) {
		this.promotionOuterList = promotionOuterList;
	}

	public String getTitleName() {
		return titleName;
	}

	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getSendWay() {
		return sendWay;
	}

	public void setSendWay(String sendWay) {
		this.sendWay = sendWay;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDomainSrc() {
		return domainSrc;
	}

	public void setDomainSrc(String domainSrc) {
		this.domainSrc = domainSrc;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String[] getQty() {
		return qty;
	}

	public void setQty(String[] qty) {
		this.qty = qty;
	}

	public String[] getSpecId() {
		return specId;
	}

	public void setSpecId(String[] specId) {
		this.specId = specId;
	}

	public PromotionPresentService getPromotionPresentService() {
		return promotionPresentService;
	}

	public void setPromotionPresentService(PromotionPresentService promotionPresentService) {
		this.promotionPresentService = promotionPresentService;
	}

	public Menu getMenuChannel() {
		return menuChannel;
	}

	public void setMenuChannel(Menu menuChannel) {
		this.menuChannel = menuChannel;
	}

	public Menu getMenuDomain() {
		return menuDomain;
	}

	public void setMenuDomain(Menu menuDomain) {
		this.menuDomain = menuDomain;
	}

	public Menu getMenuBudget() {
		return menuBudget;
	}

	public void setMenuBudget(Menu menuBudget) {
		this.menuBudget = menuBudget;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getDel() {
		return del;
	}

	public void setDel(String del) {
		this.del = del;
	}

	public String getGiftId() {
		return giftId;
	}

	public void setGiftId(String giftId) {
		this.giftId = giftId;
	}

	public List<PromotionPresent> getPromotionPresentList() {
		return promotionPresentList;
	}

	public void setPromotionPresentList(List<PromotionPresent> promotionPresentList) {
		this.promotionPresentList = promotionPresentList;
	}

	public String getProdutId() {
		return produtId;
	}

	public void setProdutId(String produtId) {
		this.produtId = produtId;
	}

}
